package com.shqcjd.preinstallglass.ui.adapter;

import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shqcjd.preinstallglass.R;
import com.shqcjd.preinstallglass.model.pojo.EquipmentAbnormalBean;
import com.shqcjd.preinstallglass.ui.activity.EquipmentAbnormalActivity;

import java.util.ArrayList;
import java.util.List;

public class EquipmentAbnormalAdapter extends BaseQuickAdapter<EquipmentAbnormalBean.EquipmentPartBean,BaseViewHolder> {
    private EquipmentAbnormalActivity context;
    private int epId;
    public EquipmentAbnormalAdapter(@Nullable List<EquipmentAbnormalBean.EquipmentPartBean> data, EquipmentAbnormalActivity context, int epId) {
        super(R.layout.item_equipment_abnormal, data);
        this.context = context;
        this.epId = epId;
    }

    private List<Integer> scrollList = new ArrayList<>();

    @Override
    protected void convert(BaseViewHolder helper, EquipmentAbnormalBean.EquipmentPartBean item) {

        helper.setText(R.id.tv_item_equipment_abnormal_title, item.equipment_part_name);

        LinearLayout mLinearLayout = helper.getView(R.id.ll_item_equipment_abnormal);
        if(mLinearLayout.getChildCount()==0){
            for(int i=0; i<item.equipment_part_item.size(); i++){
                View vChild = LayoutInflater.from(context).inflate(R.layout.item_equipment_abnormal_item, null);
                LinearLayout ll_item_equipment_abnormal = vChild.findViewById(R.id.ll_item_equipment_abnormal);
                TextView tv_item_equipment_abnormal_item_name = vChild.findViewById(R.id.tv_item_equipment_abnormal_item_name);
                TextView tv_item_equipment_abnormal_item_state = vChild.findViewById(R.id.tv_item_equipment_abnormal_item_state);
                TextView tv_item_equipment_abnormal_item_last = vChild.findViewById(R.id.tv_item_equipment_abnormal_item_last);
                tv_item_equipment_abnormal_item_name.setText(item.equipment_part_item.get(i).equipment_part_item_name);
                tv_item_equipment_abnormal_item_state.setText(item.equipment_part_item.get(i).stand);
                tv_item_equipment_abnormal_item_last.setText("上一次结果\n"+item.equipment_part_item.get(i).last_check_info);
                RadioButton rb_item_equipment_abnormal_item_true = vChild.findViewById(R.id.rb_item_equipment_abnormal_item_true);
                RadioButton rb_item_equipment_abnormal_item_false = vChild.findViewById(R.id.rb_item_equipment_abnormal_item_false);
                RadioGroup rg_equipment_abnormal_item = vChild.findViewById(R.id.rg_equipment_abnormal_item);
                rg_equipment_abnormal_item.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {

                    }
                });

                if(item.equipment_part_item.get(i).last_check_info.equals("异常")){
                    rb_item_equipment_abnormal_item_false.setChecked(true);
                }else{
                    rb_item_equipment_abnormal_item_true.setChecked(true);
                }
                mLinearLayout.addView(vChild);
                int finalI = i;
                rg_equipment_abnormal_item.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if(checkedId==R.id.rb_item_equipment_abnormal_item_true){
                            context.putCheckResultHttp(epId, "", 1, item.equipment_part_item.get(finalI).equipment_part_item_id);
                        }else{
                            context.putCheckResultHttp(epId, "", -1, item.equipment_part_item.get(finalI).equipment_part_item_id);
                        }
                        int type = scrollList.get((Integer) group.getTag()+1==scrollList.size()?(Integer) group.getTag():(Integer) group.getTag()+1);//判断当前选中的布局的下一个是否是列表的最后一个
                        srollRvHeight(ll_item_equipment_abnormal, scrollList.get(type)==0?true:false);
                        context.focusItem((Integer) group.getTag());
                    }
                });
                rg_equipment_abnormal_item.setTag(context.radioGroupList.size());
                context.radioGroupList.add(rg_equipment_abnormal_item);
                context.linearList.add(ll_item_equipment_abnormal);
                scrollList.add(i);
            }
        }

        if(helper.getAdapterPosition()==0&&context.oldFocusPos==-1){
            context.focusItem(-1);
        }
    }

    private void srollRvHeight(LinearLayout linearLayout, boolean type){
        int[] location = new  int[2] ;
        linearLayout.getLocationOnScreen(location);
        context.srollRv(location[1],type?linearLayout.getHeight()+38:linearLayout.getHeight());
    }
}
